<template>
	<div id="detail">
		<detail-nav-bar @itemClick="itemClick" ref="nav"></detail-nav-bar>
    <scroll class="detail-scroll" ref="scroll" :probetype="3" @scrollY="scrollY">
    <detail-swiper :swiperList="topImages" ref="swi"></detail-swiper>
    <detail-base-info :goods="goods"></detail-base-info>
    <detail-shop :shop="shop"></detail-shop>
    <detail-goods-info :detail-info="detail" @imageLoad="imgload"></detail-goods-info>
    <detail-param-info :paramInfo="paramInfo" ref="param"/>
    <detail-comment-info :commentInfo="commentInfo" ref="comment"/>
    <detail-recommend :goods="recommend" ref="recommend"/>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"/>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
		<!-- <toast :message="toastMsg" :isShow="toastShow"/> -->
	</div>
</template>

<script>
	import detailNavBar from './childComps/detailNavBar.vue'
  import detailSwiper from "./childComps/detailswiper.vue"
  import detailBaseInfo from './childComps/detailBaseInfo.vue'
  import detailShop from './childComps/DetailShopInfo.vue'
  import detailGoodsInfo from './childComps/DetailGoodsInfo.vue'
  import detailParamInfo from './childComps/DetailParamInfo.vue'
  import detailCommentInfo from './childComps/DetailCommentInfo.vue'
  import detailBottomBar from './childComps/DetailBottomBar.vue'
  
  import Scroll from 'components/common/scroll/scroll'
	// import toast from 'components/common/toast/toast.vue'
  import detailRecommend from 'components/content/goods/goodsList.vue'
  
	
  import { backTopMixin } from "@/common/mixin.js"
  import {debounce} from "@/common/utils";
  import { getDetail,Goods,Shop,GoodsParam,getRecommend } from "network/detail.js"
	
	import {mapActions} from "vuex"
	export default {
		name:"Detail",
		components:{
			detailNavBar,
      detailSwiper,
      detailBaseInfo,
      detailShop,
      Scroll,
      detailGoodsInfo,
      detailParamInfo,
      detailCommentInfo,
      detailRecommend,
      detailBottomBar,
			toast
		},
    mixins:[backTopMixin],
		data(){
			return{
				iid:null,
        topImages:[],
        goods:{},
        shop:{},
        detail:{},
        paramInfo:{},
        commentInfo:{},
        recommend:[],
				themeTopYs:[],
        getThemeTopYs:null,
        currentIndex:0,
				toastMsg:'',
				toastShow:false
			}
		},
    mounted() {
      this.getThemeTopYs = debounce(()=>{
        this.themeTopYs=[]
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.param.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
        // console.log(this.themeTopYs);
      })
    },
		methods:{
			...mapActions(["addCart"]),
      addToCart(){
        let product = {}
        product.price = this.goods.realPrice
        product.desc = this.goods.desc
        product.title = this.goods.title
        product.iid = this.iid
        product.img = this.topImages[0]
        
				this.addCart(product).then(res => {
					// console.log(res);
					// this.toastMsg = res
					// this.toastShow = true
					
					// setTimeout(()=>{
					// 	this.toastShow = false
					// },1500)
					
					this.$toast.show(res, 2000)
				})
				
        // this.$store.commit('addCart',product)
    //     this.$store.dispatch('addCart', product).then(res => {
				// 	console.log(res);
				// })
      },
      scrollY(options){
        this.demo(options)
        // 获取y值
        let saveY = -options.y
        // console.log(saveY);
        // 获取数组长度
        let length = this.themeTopYs.length
        
        for(let i =3;i>=0;i--){
          // console.log(i);
          if(saveY >= this.themeTopYs[i]){
            
            this.currentIndex = i
            // console.log(i);
            this.$refs.nav.currentIndex = this.currentIndex
            break
          }
  // if (this.currentIndex !== i && ((i < length-1 && saveY >= this.themeTopYs[i] && saveY < this.themeTopYs[i+1]) || (i === length-1 && saveY >= this.themeTopYs[i]))){
  //           this.currentIndex = i
  //           // console.log(i);
  //           this.$refs.nav.currentIndex = this.currentIndex
  //         }
        }
      },
      imgload(){
        this.getThemeTopYs()
      },
			itemClick(index){
				this.$refs.scroll.backtop(0, -this.themeTopYs[index],200)
			}
		},
		created() {
			this.iid= this.$route.params.iid
    
      getDetail(this.iid).then(res => {
        let data = res.result
        this.topImages = data.itemInfo.topImages
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
        this.shop = new Shop(data.shopInfo)
        this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)
        
        if(data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }
        this.detail = data.detailInfo
      })
      getRecommend().then(res => {
  
        this.recommend = res.data.list
      })
		},

		
	}
</script>

<style>
	#detail{
    height: 100vh;
  }
  .detail-scroll{
    height: calc(100% - 44px - 58px);
    overflow: hidden;
  }
</style>
